<template>
  <div class="membership-page">
    <!-- 会员卡片轮播 -->
    <section class="membership-cards">
      <swiper
        :slides-per-view="1.2"
        :space-between="15"
        :centered-slides="true"
        @swiper="onSwiper"
        @slideChange="onSlideChange"
      >
        <swiper-slide v-for="card in memberCards" :key="card.id" class="card-slide">
          <div class="member-card" :class="card.level.toLowerCase()">
            <div class="card-header">
              <img :src="card.shopLogo" :alt="card.shopName" class="shop-logo">
              <span class="shop-name">{{ card.shopName }}</span>
            </div>
            <div class="card-body">
              <div class="level-info">
                <span class="level-name">{{ card.level }}会员</span>
                <span class="valid-date">有效期至：{{ card.expireDate }}</span>
              </div>
              <div class="member-benefits">
                <div class="benefit-item" v-for="benefit in card.benefits" :key="benefit.name">
                  <i :class="benefit.icon"></i>
                  <span>{{ benefit.name }}</span>
                </div>
              </div>
            </div>
            <div class="card-footer">
              <div class="points-info">
                <span class="points">{{ card.points }}</span>
                <span class="points-label">可用积分</span>
              </div>
              <button class="renew-btn" @click="showRenewDialog(card)">续费会员</button>
            </div>
          </div>
        </swiper-slide>
      </swiper>
    </section>

    <!-- 当前会员权益 -->
    <section class="current-benefits" v-if="currentCard">
      <h2 class="section-title">会员权益</h2>
      <div class="benefits-grid">
        <div v-for="benefit in currentCard.detailedBenefits" 
             :key="benefit.id" 
             class="benefit-card">
          <div class="benefit-icon">
            <i :class="benefit.icon"></i>
          </div>
          <div class="benefit-info">
            <h3>{{ benefit.name }}</h3>
            <p>{{ benefit.description }}</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 会员等级说明 -->
    <section class="level-description" v-if="currentCard">
    <h2 class="section-title">等级说明</h2>
    <div class="level-progress">
      <div class="progress-track">
        <div class="progress-bar" :style="{ width: progressWidth }"></div>
      </div>
      <div class="level-nodes">
        <div 
          v-for="(level, index) in currentCard.levelSystem" 
          :key="level.name"
          class="level-node"
          :class="{ 
            active: level.points <= currentCard.points,
            'first-node': index === 0,
            'last-node': index === currentCard.levelSystem.length - 1
          }"
          :style="{ left: `${(level.points / maxPoints) * 100}%` }"
        >
          <div class="node-point"></div>
          <div class="node-info">
            <span class="node-name">{{ level.name }}</span>
            <span class="node-points">{{ level.points }}分</span>
          </div>
        </div>
      </div>
    </div>
    <div class="level-tips">
      <p v-if="pointsToNextLevel > 0">
        距离下一等级还需 <span class="highlight">{{ pointsToNextLevel }}</span> 积分
      </p>
      <p v-else>
        已达到最高等级
      </p>
    </div>
  </section>

    <!-- 近期优惠 -->
    <section class="recent-promotions" v-if="currentCard">
      <h2 class="section-title">近期优惠</h2>
      <div class="promotion-list">
        <div v-for="promo in currentCard.promotions" 
             :key="promo.id" 
             class="promotion-item">
          <img :src="promo.image" :alt="promo.name">
          <div class="promotion-info">
            <h3>{{ promo.name }}</h3>
            <p>{{ promo.description }}</p>
            <div class="promotion-time">
              <i class="icon-time"></i>
              <span>{{ promo.validPeriod }}</span>
            </div>
          </div>
          <button class="use-btn" @click="usePromotion(promo)">立即使用</button>
        </div>
      </div>
    </section>

    <!-- 续费弹窗 -->
    <van-dialog v-model:show="showRenew" title="会员续费" class="renew-dialog">
      <div class="renew-content">
        <div class="current-info">
          <p>当前会员：{{ selectedCard?.level }}会员</p>
          <p>有效期至：{{ selectedCard?.expireDate }}</p>
        </div>
        <div class="renew-options">
          <div 
            v-for="option in renewOptions" 
            :key="option.months"
            class="renew-option"
            :class="{ active: selectedOption === option }"
            @click="selectedOption = option"
          >
            <div class="option-header">
              <span class="months">{{ option.months }}个月</span>
              <span class="tag" v-if="option.tag">{{ option.tag }}</span>
            </div>
            <div class="option-price">
              <span class="currency">¥</span>
              <span class="amount">{{ option.price }}</span>
            </div>
            <div class="original-price" v-if="option.originalPrice">
              原价: ¥{{ option.originalPrice }}
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <div class="total-price">
            <span>实付金额：</span>
            <span class="price">¥{{ selectedOption?.price || 0 }}</span>
          </div>
          <button class="confirm-btn" @click="confirmRenew">确认续费</button>
        </div>
      </template>
    </van-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Dialog } from 'vant'
import 'vant/lib/index.css' // 引入 Vant 样式
import 'swiper/css'
import 'swiper/css/pagination'

// 会员卡数据
const memberCards = ref([
  {
    id: 1,
    shopName: '天之道足浴店',
    shopLogo: 'https://cyj456.oss-cn-hangzhou.aliyuncs.com/clock/user/avatar/9844ccac-ce5f-4ae7-8f32-0c9ca491a235.jpg',
    level: 'gold',
    expireDate: '2024-12-31',
    points: 2800,
    benefits: [
      { name: '专属折扣', icon: 'icon-discount' },
      { name: '积分加倍', icon: 'icon-points' },
      { name: '生日礼包', icon: 'icon-gift' }
    ],
    detailedBenefits: [
      {
        id: 1,
        name: '专属折扣',
        description: '所有服务项目享受8.5折优惠',
        icon: 'icon-discount'
      },
      // ... 更多权益
    ],
    levelSystem: [
      { name: '普通会员', points: 0 },
      { name: '银卡会员', points: 1000 },
      { name: '金卡会员', points: 2000 },
      { name: '钻石会员', points: 5000 }
    ],
    promotions: [
      {
        id: 1,
        name: '会员专享特惠',
        description: '精油推拿套餐限时7折',
        image: 'https://cyj456.oss-cn-hangzhou.aliyuncs.com/clock/user/avatar/OIP.jpg',
        validPeriod: '2024.1.1-2024.1.31'
      },
      // ... 更多优惠
    ]
  },
  // ... 更多会员卡
])

// 当前显示的会员卡
const currentCard = ref(memberCards.value[0])

// 计算距离下一等级所需积分
const pointsToNextLevel = computed(() => {
  const currentPoints = currentCard.value.points
  const nextLevel = currentCard.value.levelSystem.find(level => level.points > currentPoints)
  return nextLevel ? nextLevel.points - currentPoints : 0
})

// 计算进度条宽度
const progressWidth = computed(() => {
 const currentPoints = currentCard.value.points
  return `${(currentPoints / maxPoints.value) * 100}%`
})
// 添加最大积分点数计算
const maxPoints = computed(() => {
  const levels = currentCard.value.levelSystem
  return levels[levels.length - 1].points
})


// 轮播相关
const onSwiper = (swiper) => {
  console.log(swiper)
}

const onSlideChange = (swiper) => {
  currentCard.value = memberCards.value[swiper.activeIndex]
}

// 续费相关
const showRenew = ref(false)
const selectedCard = ref(null)
const selectedOption = ref(null)

const renewOptions = ref([
  {
    months: 3,
    price: 299,
    originalPrice: 399
  },
  {
    months: 6,
    price: 499,
    originalPrice: 699,
    tag: '推荐'
  },
  {
    months: 12,
    price: 899,
    originalPrice: 1299,
    tag: '最优惠'
  }
])

const showRenewDialog = (card) => {
  selectedCard.value = card
  selectedOption.value = renewOptions.value[0]
  showRenew.value = true
}

const confirmRenew = async () => {
  if (!selectedOption.value) return
  
  try {
    // 这里添加续费逻辑
    console.log('Renewing membership:', {
      cardId: selectedCard.value.id,
      months: selectedOption.value.months,
      price: selectedOption.value.price
    })
    
    // 续费成功后关闭弹窗
    showRenew.value = false
    // 显示成功提示
    Dialog.alert({
      message: '续费成功！',
      confirmButtonText: '确定'
    })
  } catch (error) {
    console.error('Renewal failed:', error)
    Dialog.alert({
      message: '续费失败，请重试',
      confirmButtonText: '确定'
    })
  }
}

// 使用优惠
const usePromotion = (promo) => {
  console.log('Using promotion:', promo)
  // 实现优惠使用逻辑
}
</script>

<style scoped>
.membership-page {
  padding: 15px;
  background: #f5f5f5;
}

/* 会员卡样式 */
.card-slide {
  width: 100%;
  padding: 10px 0;
}

.member-card {
  background: linear-gradient(135deg, #373B44 0%, #4286f4 100%);
  border-radius: 12px;
  padding: 20px;
  color: #fff;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.member-card.gold {
  background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
}

.member-card.platinum {
  background: linear-gradient(135deg, #BCC6CC 0%, #EAEAEA 100%);
}

.member-card.diamond {
  background: linear-gradient(135deg, #B9F2FF 0%, #86A8E7 100%);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.shop-logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.3);
}

.shop-name {
  font-size: 18px;
  font-weight: 500;
}

.level-info {
  margin-bottom: 15px;
}

.level-name {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
  display: block;
}

.valid-date {
  font-size: 14px;
  opacity: 0.8;
}

.member-benefits {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.benefit-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.points-info {
  text-align: center;
}

.points {
  font-size: 24px;
  font-weight: bold;
  display: block;
}

.points-label {
  font-size: 12px;
  opacity: 0.8;
}

.renew-btn {
  padding: 8px 20px;
  background: rgba(255,255,255,0.2);
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 20px;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.renew-btn:hover {
  background: rgba(255,255,255,0.3);
}

/* 会员权益样式 */
.section-title {
  font-size: 18px;
  font-weight: bold;
  margin: 20px 0 15px;
}

.benefits-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.benefit-card {
  background: #fff;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  gap: 10px;
}

.benefit-icon {
  width: 40px;
  height: 40px;
  background: #f5f5f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ff4d4f;
}

.benefit-info h3 {
  font-size: 16px;
  margin: 0 0 5px;
}

.benefit-info p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 等级进度条样式 */
.level-progress {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 15px;
}

.progress-bar {
  position: relative;
  height: 4px;
  background: #eee;
  border-radius: 2px;
  margin: 30px 0;
}

.progress {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: #ff4d4f;
  border-radius: 2px;
  transition: width 0.3s;
}

.level-node {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.node-point {
  width: 12px;
  height: 12px;
  background: #eee;
  border-radius: 50%;
  margin-bottom: 8px;
}

.level-node.active .node-point {
  background: #ff4d4f;
}

.node-name {
  font-size: 12px;
  color: #666;
  white-space: nowrap;
}

.level-tips {
  text-align: center;
  color: #666;
  font-size: 14px;
}

/* 优惠列表样式 */
.promotion-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.promotion-item {
  background: #fff;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  gap: 15px;
}

.promotion-item img {
  width: 100px;
  height: 70px;
  border-radius: 4px;
  object-fit: cover;
}

.promotion-info {
  flex: 1;
}

.promotion-info h3 {
  font-size: 16px;
  margin: 0 0 5px;
}

.promotion-info p {
  font-size: 14px;
  color: #666;
  margin: 0 0 8px;
}

.promotion-time {
  color: #999;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.use-btn {
  padding: 6px 15px;
  background: #ff4d4f;
  color: #fff;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  align-self: center;
}

/* 续费弹窗样式 */
.renew-content {
  padding: 20px;
}

.current-info {
  margin-bottom: 20px;
  color: #666;
}

.renew-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.renew-option {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

.renew-option.active {
  border-color: #ff4d4f;
  background: #fff2f0;
}

.option-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.months {
  font-size: 16px;
  font-weight: 500;
}

.tag {
  padding: 2px 6px;
  background: #ff4d4f;
  color: #fff;
  border-radius: 2px;
  font-size: 12px;
}

.option-price {
  font-size: 24px;
  font-weight: bold;
  color: #ff4d4f;
}

.currency {
  font-size: 16px;
}

.original-price {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
  margin-top: 5px;
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-top: 1px solid #eee;
}

.total-price {
  color: #666;
}

.total-price .price {
  color: #ff4d4f;
  font-size: 20px;
  font-weight: bold;
}

.confirm-btn {
  padding: 8px 24px;
  background: #ff4d4f;
  color: #fff;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

@media screen and (max-width: 768px) {
  .benefits-grid {
    grid-template-columns: 1fr;
  }
  
  .renew-options {
    grid-template-columns: 1fr;
  }
  
  .promotion-item {
    flex-direction: column;
  }
  
  .promotion-item img {
    width: 100%;
    height: 160px;
  }
  
  .use-btn {
    width: 100%;
  }
}
.level-progress {
  background: #fff;
  border-radius: 8px;
  padding: 40px 20px;
  margin: 20px 0;
  position: relative;
}

.progress-track {
  height: 4px;
  background: #eee;
  border-radius: 2px;
  position: relative;
  margin: 20px 0 30px;
}

.progress-bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: #ff4d4f;
  border-radius: 2px;
  transition: width 0.3s ease;
}

.level-nodes {
  position: relative;
  height: 60px;
}

.level-node {
  position: absolute;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60px;
}

.node-point {
  width: 12px;
  height: 12px;
  background: #eee;
  border: 2px solid #fff;
  border-radius: 50%;
  margin-bottom: 8px;
  transition: all 0.3s ease;
}

.level-node.active .node-point {
  background: #ff4d4f;
  border-color: #ffe7e7;
}

.node-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.node-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
  white-space: nowrap;
}

.node-points {
  font-size: 12px;
  color: #999;
  white-space: nowrap;
}

/* 首尾节点特殊处理 */
.level-node.first-node {
  left: 0 !important;
  transform: none;
  align-items: flex-start;
}

.level-node.last-node {
  right: 0 !important;
  left: auto !important;
  transform: none;
  align-items: flex-end;
}

.level-tips {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-top: 20px;
}

.level-tips .highlight {
  color: #ff4d4f;
  font-weight: bold;
  padding: 0 4px;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .level-progress {
    padding: 30px 10px;
  }

  .level-node {
    width: 50px;
  }

  .node-name {
    font-size: 12px;
  }

  .node-points {
    font-size: 10px;
  }
}
</style>